//---------------------------------------------------
//     Copyright (c) 2007 Jeffrey Bazinet, VWD-CMS 
//     http://www.vwd-cms.com/  All rights reserved.
//---------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;

namespace VwdCms
{
	public class ImageZoom : Panel, INamingContainer
	{
		protected HtmlImage imgMain;
		protected HtmlGenericControl divImage;

		private bool _controlsBuilt;

		protected override void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(ImageZoom_Load);
			base.OnInit(e);
		}

		private void ImageZoom_Load(object sender, EventArgs e)
		{
			BuildCompositeControls();
		}

		protected override void OnPreRender(EventArgs e)
		{
			RegisterControlClientScript();
			base.OnPreRender(e);
		}

		public string ImageUrl
		{
			get 
			{
				BuildCompositeControls();
				return this.Image.Src; 
			}
			set 
			{
				BuildCompositeControls();
				this.Image.Src = value;
			}
		}

		public HtmlImage Image
		{
			get { return this.imgMain; }
		}

		private void BuildCompositeControls()
		{
			if (!_controlsBuilt)
			{
				_controlsBuilt = true;

				this.divImage = new HtmlGenericControl("div");
				this.divImage.ID = "divImage";

				this.imgMain = new HtmlImage();
				this.imgMain.ID = "imgMain";
				this.imgMain.Style.Add("border", "solid 1px #333333");
				this.divImage.Controls.Add(this.imgMain);

				this.Controls.Add(this.divImage);
			}
		}

		private void RegisterControlClientScript()
		{
			StringBuilder sb = new StringBuilder();
			const string newline = "\r\n";

			sb.Append("<script type=\"text/javascript\"> <!-- ");
			sb.Append(newline);

			sb.Append("function ");
			sb.Append(this.ClientID);
			sb.Append("_setImage(src){");
			sb.Append("try{");
			sb.Append("var img=document.getElementById('");
			sb.Append(this.imgMain.ClientID);
			sb.Append("');");
			sb.Append("img.removeAttribute('width');");
			sb.Append("img.src=src;");
			sb.Append("}catch(e){alert('setImage error: ' + e.message);}");
			sb.Append("}");
			sb.Append(newline);

			sb.Append("function ");
			sb.Append(this.ClientID);
			sb.Append("_zoomIn(){");
			sb.Append("try{");
			sb.Append("var img=document.getElementById('");
			sb.Append(this.imgMain.ClientID);
			sb.Append("');img.width=img.width*1.25;");
			sb.Append("}catch(e){alert('zoomIn error: ' + e.message);}");
			sb.Append("}");
			sb.Append(newline);

			sb.Append("function ");
			sb.Append(this.ClientID);
			sb.Append("_zoomOut(){");
			sb.Append("try{");
			sb.Append("var img=document.getElementById('");
			sb.Append(this.imgMain.ClientID);
			sb.Append("');img.width=img.width*0.75;");
			sb.Append("}catch(e){alert('zoomOut error: ' + e.message);}");
			sb.Append("}");
			sb.Append(newline);

			sb.Append("function ");
			sb.Append(this.ClientID);
			sb.Append("_actualSize(){");
			sb.Append("try{");
			sb.Append("var img=document.getElementById('");
			sb.Append(this.imgMain.ClientID);
			sb.Append("');");
			sb.Append("img.removeAttribute('width');");
			sb.Append("}catch(e){alert('actualSize error: ' + e.message);}");
			sb.Append("}");
			sb.Append(newline);

			sb.Append("function ");
			sb.Append(this.ClientID);
			sb.Append("_bestFit(){");
			sb.Append("try{");
			sb.Append("var iz=document.getElementById('");
			sb.Append(this.ClientID);
			sb.Append("');var img=document.getElementById('");
			sb.Append(this.imgMain.ClientID);
			sb.Append("');");
			sb.Append("var cw=iz.clientWidth;while(cw==0){iz=iz.parentElement;cw=iz.clientWidth;}");
			sb.Append("var ch=iz.clientHeight;");
			sb.Append("img.removeAttribute('width');");
			sb.Append("var ar=img.width/img.height;");
			sb.Append("if (ar>=1.0) {img.width=cw-2;}");
			sb.Append("else {img.width=Math.floor(ch*ar)-2;}");
			sb.Append("}catch(e){alert('bestFit error: ' + e.message);}");
			sb.Append("}");
			sb.Append(newline);

			sb.Append("// -->");
			sb.Append(newline);
			sb.Append("</script>");
			sb.Append(newline);

			this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
				this.ClientID + "_ClientScript", sb.ToString());
		}
	}
}